/*
Universal Molotilka 1.0
Copyright (c) 2011, 2012, Alexey Silaev ArchFurry@gmail.com
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
		list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, 
		this list of conditions and the following disclaimer in the documentation
		and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "cipher.h"
#include "random.h"
inline uint32_t UMcipher_modify
 (uint32_t a, uint32_t b, struct UMcipher_context *umc)
 {
	uint8_t k[4], m[4];
	
	k[0]=(a&0xff000000)>>24;
	k[1]=(a&0x00ff0000)>>16;
	k[2]=(a&0x0000ff00)>>8;
	k[3]=(a&0x000000ff);
	
	m[0]=(b&0xff000000)>>24;
	m[1]=(b&0x00ff0000)>>16;
	m[2]=(b&0x0000ff00)>>8;
	m[3]=(b&0x000000ff);
	
	k[0]=umc->table[k[0]*256+m[0]];
	k[1]=umc->table[k[1]*256+m[1]];
	k[2]=umc->table[k[2]*256+m[2]];
	k[3]=umc->table[k[3]*256+m[3]];
	
	return (k[3]<<24)+(k[0]<<16)+(k[1]<<8)+(k[2]);
 }
//struct UMcipher_context *umc

// Blck
inline void UMcipher_block_encipher
 (uint32_t *block, struct UMcipher_context *umc)
 {
	size_t i;
	uint32_t tmp;

	umc->key = UMcipher_modify
		(umc->key, umc->sub_key, umc);
		
	for(i=0; i < umc->block_size; i++){
		umc->sub_key = UMcipher_modify
			(umc->sub_key, umc->key | umc->sub_key, umc);
		tmp = block[i];
		block[i] ^= umc->key;
		umc->key = UMcipher_modify
			(umc->key, tmp & umc->sub_key, umc);
	}
 }

inline void UMcipher_block_decipher
 (uint32_t *block, struct UMcipher_context *umc)
 {
	size_t i;

	umc->key = UMcipher_modify
		(umc->key, umc->sub_key, umc);
		
	for(i=0; i < umc->block_size; i++){
		umc->sub_key = UMcipher_modify
			(umc->sub_key, umc->key | umc->sub_key, umc);
		block[i] ^= umc->key;
		umc->key = UMcipher_modify
			(umc->key, block[i] & umc->sub_key, umc);
	}
 }
//Block ciper end

//File encryption
void UMcipher_file_encipher
 (char *input_file_name, char *output_file_name, struct UMcipher_context *umc)
 {
	FILE *input_file, *output_file;
	size_t file_size, i, block_num, block_bytes;
	uint8_t *block_8;
	uint32_t *block_32;
	uint8_t offset=0; //for SAFE_FILE_ENC
	
	
	block_bytes = umc->block_size * sizeof(uint32_t); // for fread
	
	// File open
	input_file = fopen(input_file_name, "rb");
	if (input_file == NULL){
		perror(input_file_name);
		exit(1);
	}
	
	output_file = fopen(output_file_name, "wb");
	if (output_file == NULL){
		perror(output_file_name);
		exit(1);
	}
	
	//malloc
	block_8 = (uint8_t *) malloc (block_bytes);
	if(block_8 == NULL){
		perror("UMcipher_file_encipher:malloc_block_8:");
		exit(1);
	}
	
	block_32 = (uint32_t *) malloc (block_bytes);
	if(block_32 == NULL){
		perror("UMcipher_file_encipher:malloc_block_32:");
		exit(1);
	}

	//Get file _size
	file_size = fsize(input_file);
	
	if(umc->flag == SAFE_FILE_ENC){// for 8 random bytes
		offset=8;
		file_size+=8;
	}

	if(file_size%block_bytes==0)
		block_num = file_size / block_bytes;
	else block_num = (file_size / block_bytes) + 1;

	for(i=0; i<block_num; i++){

		if((umc->flag == SAFE_FILE_ENC) && (i == 1))
			offset=0;

		if(file_size%block_bytes!=0 && i==block_num-1)
			fread(block_8+offset, 1, (file_size-offset)%block_bytes, input_file);
		else fread(block_8+offset, 1, block_bytes-offset, input_file);

		block_8_to_32(block_8, block_32, block_bytes);
		
		if(umc->flag == SAFE_FILE_ENC)
			if(i==0){
				block_32[0] = UMrandom_32();
				block_32[1] = UMrandom_32();
			}
	
		UMcipher_block_encipher(block_32, umc);
		
		block_32_to_8(block_8, block_32, block_bytes);
		
		if(file_size%block_bytes!=0 && i==block_num-1)
			fwrite(block_8, 1, file_size%block_bytes, output_file);
		else fwrite(block_8, 1, block_bytes, output_file);
	}
	free(block_8);
	free(block_32);
	fclose(input_file);
	fclose(output_file);
 }
 
void UMcipher_file_decipher
 (char *input_file_name, char *output_file_name, struct UMcipher_context *umc)
 {
	FILE *input_file, *output_file;
	size_t file_size, i, block_num, block_bytes;
	uint8_t *block_8;
	uint32_t *block_32;
	uint8_t offset=0; //for SAFE_FILE_ENC
	
	
	block_bytes = umc->block_size * sizeof(uint32_t); // for fread
	
	// File open
	input_file = fopen(input_file_name, "rb");
	if (input_file == NULL){
		perror(input_file_name);
		exit(1);
	}
	
	output_file = fopen(output_file_name, "wb");
	if (output_file == NULL){
		perror(output_file_name);
		exit(1);
	}
	
	//malloc
	block_8 = (uint8_t *) malloc (block_bytes);
	if(block_8 == NULL){
		perror("UMcipher_file_decipher:malloc_block_8:");
		exit(1);
	}
	
	block_32 = (uint32_t *) malloc (block_bytes);
	if(block_32 == NULL){
		perror("UMcipher_file_decipher:malloc_block_32:");
		exit(1);
	}

	//Get file _size
	file_size = fsize(input_file);
	
	if(umc->flag == SAFE_FILE_ENC){// for 8 random bytes
		offset=8;
	}

	if(file_size%block_bytes==0)
		block_num = file_size / block_bytes;
	else block_num = (file_size / block_bytes) + 1;

	for(i=0; i<block_num; i++){

		if((umc->flag == SAFE_FILE_ENC) && (i == 1))
			offset=0;

		if(file_size%block_bytes!=0 && i==block_num-1)
			fread(block_8, 1, file_size%block_bytes, input_file);
		else fread(block_8, 1, block_bytes, input_file);

		block_8_to_32(block_8, block_32, block_bytes);
	
		UMcipher_block_decipher(block_32, umc);
		
		block_32_to_8(block_8, block_32, block_bytes);
		
		if(file_size%block_bytes!=0 && i==block_num-1)
			fwrite(block_8+offset, 1, (file_size-offset)%block_bytes, output_file);
		else fwrite(block_8+offset, 1, block_bytes-offset, output_file);
	}
	free(block_8);
	free(block_32);
	fclose(input_file);
	fclose(output_file);
 }
